package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    DeptService deptService;

    // 查询部门
    @GetMapping
    public R listdept() {
        List<Dept> depts = deptService.listDept();
        return R.ok(depts);
    }



    // 新增部门
    @PostMapping
    public R adddept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return R.ok();
    }

    // 根据ID查询部门
    @GetMapping("/{id}")
    public R getdept(@PathVariable Integer id){
        Dept deptById = deptService.getDeptById(id);
        return R.ok(deptById);
    }

    // 根据ID修改部门
    @PutMapping
    public R updatedept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return R.ok();
    }

    // 根据ID删除部门
    @DeleteMapping
    public R deletedept(Integer id) {
        deptService.deleteDept(id);
        return R.ok();
    }

}
